﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View.Event
{
    public partial class frmEvent : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }


        protected void FormSave(object sender, AjaxEventArgs e)
        {
            try
            {
                string id = this.txt_eventId.Text;

                if (id.Equals(""))
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {
                        gmevent evento = new gmevent();
                        evento.NMEVENT = this.eventName.Text.ToString();
                        evento.DTEVENT = this.eventDate.SelectedDate;
                        evento.DSEVENT = this.eventDescription.Text.ToString();

                        Controller.Event.addEvent(evento);
                        ClearWindow(Controls);
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Cadastro efetuado com sucesso!", IconCls = "", Clear2 = true });
                    }
                }
                else
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {
                        gmevent evento = Controller.Event.getByEvent(int.Parse(id));
                        evento.NMEVENT = this.eventName.Text.ToString();
                        evento.DTEVENT = this.eventDate.SelectedDate;
                        evento.DSEVENT = this.eventDescription.Text.ToString();
                        Controller.Event.updateEvent(evento);
                        this.winEvent.Hide();
                        ClearWindow(Controls);
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        Ext.Msg.Alert("Alerta!", "Evento <br>" + evento.NMEVENT + " </br> atualizado com sucesso!").Show();
                        FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Evento atualizado com sucesso!", IconCls = "", Clear2 = true });

                    }
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void ShowEvent()
        {
            this.winEvent.Show();
        }

        public void SetEvent(gmevent evento)
        {
            this.txt_eventId.Text = evento.CDEVENT.ToString();
            this.eventName.Text = evento.NMEVENT;
            this.eventDate.SelectedDate = evento.DTEVENT;
            this.eventDescription.Text = evento.DSEVENT;
        }

        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winEvent.Hide();
            ClearWindow(Controls);
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).SetValue("");
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is DateField)
                {
                    ((DateField)c).SetValue("");
                }
                if (c is TextArea)
                {
                    ((TextArea)c).Text = "";
                    ((TextArea)c).Visible = false;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                this.winEvent.Restore();
                ClearWindow(c.Controls);
            }
        }
    }
}